package com.yxq.mdt.app.modules.socialcontact.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.modules.socialcontact.bean.MainSocialContactMultiBaseBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialContactTabMenuBean;
import com.yxq.mdt.app.utils.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.List;

import butterknife.BindView;

/**
 * @author L
 * @time 2019-12-17
 * @describe 社区适配器$
 */
public class MainSocialContactListAdapter extends BaseMultiItemQuickAdapter<MainSocialContactMultiBaseBean, BaseViewHolder> {
    FragmentManager fragmentManager;

    public MainSocialContactListAdapter(FragmentManager fragmentManager, List data) {
        super(data);
        this.fragmentManager = fragmentManager;
        addItemType(MainSocialContactMultiBaseBean.ITEM_TYPE_RECOMMEND_USER, R.layout.item_social_contact_recommend_user_layout);
        addItemType(MainSocialContactMultiBaseBean.ITEM_TYPE_RECOMMEND_GROUP, R.layout.item_social_contact_recommend_group_layout);
        addItemType(MainSocialContactMultiBaseBean.ITEM_TYPE_RECOMMEND_TAB_MENU, R.layout.item_social_contact_tab_menu_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, MainSocialContactMultiBaseBean item) {
        switch (item.getItemType()) {
            case MainSocialContactMultiBaseBean.ITEM_TYPE_RECOMMEND_USER:
                break;
            case MainSocialContactMultiBaseBean.ITEM_TYPE_RECOMMEND_GROUP:
                break;
            case MainSocialContactMultiBaseBean.ITEM_TYPE_RECOMMEND_TAB_MENU:
                SocialContactTabMenuBean socialContactTabMenuBean = (SocialContactTabMenuBean) item;
                MagicIndicator magicIndicator = helper.getView(R.id.magic_indicator);
                ViewPager viewPager = helper.getView(R.id.magic_view_pager);
                ExamplePagerAdapter mExamplePagerAdapter = new ExamplePagerAdapter(fragmentManager, socialContactTabMenuBean.getFragments(), socialContactTabMenuBean.getDataList());
                viewPager.setAdapter(mExamplePagerAdapter);
                initMagicIndicator(socialContactTabMenuBean.getDataList(), viewPager, magicIndicator);
                break;
        }
    }

    private void initMagicIndicator(List<String> mDataList, ViewPager viewPager, MagicIndicator magicIndicator) {
        CommonNavigator commonNavigator = new CommonNavigator(mContext);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context); //使用自定义
                simplePagerTitleView.setNormalColor(Color.BLACK);
                simplePagerTitleView.setSelectedColor(Color.BLACK);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(20);
                simplePagerTitleView.setOnClickListener(v -> viewPager.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                linePagerIndicator.setLineWidth(UIUtil.dip2px(context, 40));
                linePagerIndicator.setLineHeight(UIUtil.dip2px(context, 2));
                linePagerIndicator.setColors(Color.parseColor("#FFD800"));
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(mContext, 0);
            }
        });
        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(0.1f));
        fragmentContainerHelper.setDuration(0);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
        viewPager.setOffscreenPageLimit(5);

    }
}
